package com.meituan.verintreport.task;

import com.meituan.verintreport.db.dao.AnswersMapper;
import com.meituan.verintreport.db.dto.AnswersDto;
import com.meituan.verintreport.db.verint.VerintBaseTableDao;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * 全量数据同步
 */
@Slf4j
@Data
@Component
@ConfigurationProperties(prefix = "base-table.answers")
public class BaseTableTaskAnswers extends BaseTableTask {
    @Resource
    private AnswersMapper answersMapper;

    @Resource
    private VerintBaseTableDao verintBaseTableDao;

    @Value("${report.timezone}")
    private String reportTimeZone;

    @Value("${base-table.answers.maxMinutesPerTime}")
    private int maxMinutesPerTime = 15;

    /**
     * 每天12点  删除之前的数据，同步全量
     */
    @Scheduled(cron = "${base-table.answers.cron}")
    @Override
    public void execute() {
        if (!isEnable()){
            return;
        }

        // Calendar beforeTime1 = Calendar.getInstance();
        // TimeZone timezone = TimeZone.getTimeZone(reportTimeZone);
        // beforeTime1.setTimeZone(timezone);
        // beforeTime1.add(Calendar.HOUR_OF_DAY, -1);
        // beforeTime1.set(Calendar.MINUTE, 0);
        // beforeTime1.set(Calendar.SECOND, 0);
        // beforeTime1.set(Calendar.MILLISECOND, 0);
        // Date endDateTime = beforeTime1.getTime();
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // sdf.setTimeZone(timezone);
        //String endTimeStr = sdf.format(endDateTime) + ".000";
        //log.info("timezone = {} endTime = {}", timezone.getDisplayName(), endTimeStr);

        Integer rs = answersMapper.removeAnswersAll();
        log.info("remove {} data", rs);

        boolean shouldContinue;
        int pageIndex = 0;
        int pageSize = 1000;
        do {
            shouldContinue = false;


            //Integer maxAnswerId = answersMapper.getMaxAnswerIdBeforeDateTime(endTimeStr);
           // log.info("last max answer id: {}", maxAnswerId);
           // List<AnswersDto> records = verintBaseTableDao.getAnswerListBeforeDateTime(maxAnswerId, endTimeStr);

            List<AnswersDto> records = verintBaseTableDao.getAnswerListByPage(pageIndex, pageSize);
            log.info("got {} records", records.size());
            if (records.size() == 1000) {
                shouldContinue = true;
            }

            for (AnswersDto record : records) {
                try {
                    answersMapper.insert(record);
                } catch (Exception ex) {
                    log.error("Add record to db exception.", ex);
                }
            }
            pageIndex += pageSize;
        }
        while (shouldContinue);
    }
}
